package com.surekam.core.pdfbook.template;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;

import com.surekam.core.pdfbook.book.CreateBookPdf;
import com.surekam.core.pdfbook.domain.Role;

public class TemplateUtils {

	public TemplateUtils() {
		Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
				"E:/guo_sources/osc/我的Git项目/core/core/src/main/resources/templates/");

		Velocity.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
		Velocity.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
		Velocity.init();

	}

	public void createHtml(String templateName) {
		Template template = Velocity.getTemplate(templateName, "UTF-8");
		Context context = new VelocityContext();
		List<Role> addRoles = addRoles();
		context.put("nums", addRoles);
		PrintWriter writer = null;
		try {
			File f = new File("demo.html");
			if(f.exists()){
				System.out.println("Demo文件已经存在");
				f.delete();
				System.out.println("模板文件删除");
			}
			writer = new PrintWriter(new File("demo.html"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		template.merge(context, writer);
		writer.close();
		
	}
	
	public List<Role> addRoles(){
		List<Role> list = new ArrayList<Role>();
		for (int i = 0; i < 300; i++) {
			Role r = new Role();
			r.setCreateTime(new Date().toLocaleString());
			r.setRoleApplyScopeId("" + i+ new Random(1000).nextInt());
			r.setRoleId(i);
			r.setRoleName("" + i+ new Random(10001).nextInt());
			r.setRoleTypeId("" + i);
			list.add(r);
		}
		return list;
	}
}
